(function ($, Drupal, window, document, undefined) {

	$(document).ready(function() {
		var calendar = $('#calendar').fullCalendar({
		
			editable: true,
			
      events: [{title: 'test', start: '2013-09-20'}],
//      selectable: true,
      dayClick: function(date, allDay, jsEvent, view){
        $('#event-modal').modal('show');
      },
//      select: function(start, end, allDay) {
//        var title = prompt('Event Title:');
//        if (title) {
//          calendar.fullCalendar('renderEvent',
//            {
//              title: title,
//              start: start,
//              end: end,
//              allDay: allDay
//            },
//            true 
//          );
//        }
//        calendar.fullCalendar('unselect');
//      },
//  
			eventDrop: function(event, delta) {
				alert(event.title + ' was moved ' + delta + ' days\n' +
					'(should probably update your database)');
			},
			
//      loading: function(bool) {
//        if (bool) $('#loading').show();
//        else $('#loading').hide();
//      }
//      
		});



    startEndDatePicker();

    $('#event_multiday').change(function() {
      $('#event_end_date').toggle();
    });

    $("#event_start_time, #event_end_time").timePicker({
//      startTime: "02.00", // Using string. Can take string or Date object.
      endTime: new Date(0, 0, 0, 15, 30, 0), // Using Date object here.
      show24Hours: false,
//      separator: ':',
      step: 30
    });


    $('#new-event').bind('ajax:beforeSend', function() {
      $('#event-error').hide();
      $('#event-submit').button('loading');
      var repeatByDay = $('#new-event button[name="input_repeat_byday"].active').map(function() {
        return $(this).val();
      }).get();

      $('#event_repeat_byday').val(repeatByDay);
    }).bind('ajax:success', function(data, textStatus, jqXHR) {
      $('#event-modal').modal('hide');
      $('#event-submit').button('reset');
    }).bind('ajax:error', function(event, xhr, status, error) {
      if (xhr.status == 400) {
        var response = jQuery.parseJSON(xhr.responseText);
        $('#event-error').text(response.error.description);
      } else {
        $('#event-error').text('There was a problem saving. You should try again or reload the page.');
      }
      $('#event-submit').button('reset');
      $('#event-error').show();
    });

//end document ready
	});

  var startEndDatePicker = function(){
    var nowTemp = new Date();
    var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);
   $( "#event_start_date" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
      numberOfMonths: 1,
      minDate: 0,
      onClose: function( selectedDate ) {
        $( "#event_end_date" ).datepicker( "option", "minDate", selectedDate );
      }
    });
    $( "#event_end_date" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
      numberOfMonths: 1,
      onClose: function( selectedDate ) {
        $( "#event_start_date" ).datepicker( "option", "maxDate", selectedDate );
      }
    });
  };

})(jQuery, Drupal, this, this.document);
